package com.xjlsoft.weather.service.impl;

import com.xjlsoft.weather.dto.ExportDto;
import com.xjlsoft.weather.dto.RadiationDataListDto;
import com.xjlsoft.weather.dto.WeatherDataQueryDto;
import com.xjlsoft.weather.entity.RadiationData;
import com.xjlsoft.weather.mapper.RadiationDataMapper;
import com.xjlsoft.weather.service.RadiationDataService;
import com.xjlsoft.weather.util.WeatherDataUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.sql.Timestamp;
import java.text.ParseException;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 辐射数据表 服务实现类
 * </p>
 *
 * @author zhangbanglong
 * @since 2020-07-22
 */
@Service
public class RadiationDataServiceImpl extends ServiceImpl<RadiationDataMapper, RadiationData> implements RadiationDataService {
	/**
	 * 日志
	 */
	private static final Logger logger = LoggerFactory.getLogger(GroundDataServiceImpl.class);
	
	
	
	@Autowired 
	private RadiationDataMapper radiationDataMapper;

	@Value("${fileSaveDirPrefix}")
	private String fileSaveDirPrefix;// 文件存储根路径


	@Override
	public boolean saveList(List<RadiationData> listRadiationDdata) {
		boolean result = this.saveBatch(listRadiationDdata);
		return result;
	}


	@Override
	public IPage<RadiationDataListDto> getList(Page page, WeatherDataQueryDto weatherDataDto) throws ParseException {
		QueryWrapper<RadiationData> query = Wrappers.query();
		/*query.select("ground_data_id","data_query_id","create_time","data_time","station_name","province","city",
				    "cnty","town","year","mon","day","hour","lat","lon","alti","prs_sensor_alti","win_s_sensor_heigh","prs",
				     "prs_sea","tem","rhu","pre","gst");*/
        //拼接条件
        /*query.eq(StringUtils.isNotBlank(weatherDataDto.getMaleFlag()), "male_flag", personQueryParam.getMaleFlag())
                .eq(ObjectUtil.isNotNull(personQueryParam.getNationType()), "nation_type", personQueryParam.getNationType())
                .eq(ObjectUtil.isNotNull(personQueryParam.getEducationType()), "education_type", personQueryParam.getEducationType())
                .eq(ObjectUtil.isNotNull(personQueryParam.getPoliticsType()), "politics_type", personQueryParam.getPoliticsType()));*/
    
		if(StringUtils.isNotBlank(weatherDataDto.getDateStart())){
			Timestamp timestart=WeatherDataUtil.strToTimestamp(weatherDataDto.getDateStart());
			query.ge("data_time", timestart);
		}
		if(StringUtils.isNotBlank(weatherDataDto.getDateEnd())){
			Timestamp timeend=WeatherDataUtil.strToTimestamp(weatherDataDto.getDateEnd());
			query.le("data_time", timeend);
		}
        IPage<RadiationDataListDto> list =radiationDataMapper.getRadiationDatasByParams(page, query);
        		
        		//groundDataMaper.selectPage(page, query);
		return list;
	}


	@Override
	public List<RadiationData> getList(ExportDto exportDto) throws Exception {
		QueryWrapper<RadiationData> query = Wrappers.query();
	
		if(StringUtils.isNotBlank(exportDto.getDateStart())){
			Timestamp timestart=WeatherDataUtil.strToTimestamp(exportDto.getDateStart());
			query.ge("data_time", timestart);
		}
		if(StringUtils.isNotBlank(exportDto.getDateEnd())){
			Timestamp timeend=WeatherDataUtil.strToTimestamp(exportDto.getDateEnd());
			query.le("data_time", timeend);
		}
       
        List<RadiationData> list =radiationDataMapper.selectList(query);
        		//groundDataMaper.selectPage(page, query);
		return list;
	}


	@Override
	public List<Map<String, Object>> getListMap(ExportDto exportDto) throws Exception {
		QueryWrapper<RadiationData> query = Wrappers.query();
		query.select(exportDto.getSelectFilds());

		if(StringUtils.isNotBlank(exportDto.getDateStart())){
			Timestamp timestart=WeatherDataUtil.strToTimestamp(exportDto.getDateStart());
			query.ge("data_time", timestart);
		}
		if(StringUtils.isNotBlank(exportDto.getDateEnd())){
			Timestamp timeend=WeatherDataUtil.strToTimestamp(exportDto.getDateEnd());
			query.le("data_time", timeend);
		}
		 List<Map<String, Object>> map= radiationDataMapper.selectMaps(query);	
	     return map;
	}


	@Override
	public int getListMapCount(ExportDto exportDto) throws Exception {
		QueryWrapper<RadiationData> query = Wrappers.query();
		if(StringUtils.isNotBlank(exportDto.getDateStart())){
			Timestamp timestart=WeatherDataUtil.strToTimestamp(exportDto.getDateStart());
			query.ge("data_time", timestart);
		}
		if(StringUtils.isNotBlank(exportDto.getDateEnd())){
			Timestamp timeend=WeatherDataUtil.strToTimestamp(exportDto.getDateEnd());
			query.le("data_time", timeend);
		}
		int count=radiationDataMapper.selectCount(query);
		return count;
	}

}
